﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using Pathfinding;
using System.IO;
public class WayPointEditor
{
    [MenuItem("Pathfinding/New WayPoint")]
    static void NewWayPoint()
    {
        WayPointRoot wayPointRoot = GameObject.FindObjectOfType<WayPointRoot>();
        if (wayPointRoot != null)
        {
          WayPointObject newPoint =  wayPointRoot.NewWayPoint();
            newPoint.transform.position = wayPointRoot.WayPoints[wayPointRoot.WayPoints.Count - 2].transform.position;
        }
    }
    [MenuItem("Pathfinding/Clear WayPoint")]
    static void ClearWayPoint()
    {
        WayPointRoot wayPointRoot = GameObject.FindObjectOfType<WayPointRoot>();
        if (wayPointRoot != null)
        {
            wayPointRoot.ClearWayPoint();
        }
    }
    [MenuItem("Pathfinding/Save WayPoint File")]
    static void SaveWayPoint()
    {
        WayPointRoot wayPointRoot = GameObject.FindObjectOfType<WayPointRoot>();
        if (wayPointRoot != null)
        {
            string savePath = EditorUtility.SaveFilePanel("保存地图文件", Application.dataPath, "waypoints", "txt");
            File.WriteAllText(savePath, wayPointRoot.Export2LispData());
        }
    }
    [MenuItem("Pathfinding/Load WayPoint File")]
    static void LoadWayPoint()
    {
        WayPointRoot wayPointRoot = GameObject.FindObjectOfType<WayPointRoot>();
        if (wayPointRoot != null)
        {
           string loadPath = EditorUtility.OpenFilePanel("载入地图",Application.dataPath,"txt");
           Hashtable PointDic = (Hashtable)SGLisp.Helper.EvalValueFromFile(loadPath);
           wayPointRoot.LoadFromConfig(PointDic);
        }
    }
}
